/**
 *  Copyright(C) 2012-2013 Suntec(Shanghai) Software Co., Ltd.
 *  All Right Reserved.
 */
package com.cugbmao.lucene;

import java.io.File;

import org.apache.lucene.analysis.core.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * Descriptions
 * 
 * @version 2013年5月29日
 * @author Suntec
 * @since JDK1.6
 * 
 */
public class Explainer {

	/**
	 * @param args
	 */
	public static void main( String[] args ) throws Exception {
		if ( args.length != 2 ) {
			System.err.println( "Usage: Explainer <index dir> <query>" );
			System.exit( 1 );
		}

		String indexDir = args[0];
		String queryExpression = args[1];

		Directory directory = FSDirectory.open( new File( indexDir ) );
		QueryParser parser = new QueryParser( Version.LUCENE_43, "contents", new SimpleAnalyzer( Version.LUCENE_43 ) );
		Query query = parser.parse( queryExpression );

		System.out.println( "Query: " + queryExpression );

		IndexReader reader = DirectoryReader.open( directory );
		IndexSearcher searcher = new IndexSearcher( reader );
		TopDocs docs = searcher.search( query, 10 );

		for ( ScoreDoc doc : docs.scoreDocs ) {
			Explanation explanation = searcher.explain( query, doc.doc );
			System.out.println( "-----------" );
			Document d = searcher.doc( doc.doc );
			System.out.println( d.get( "title" ) );
			System.out.println( explanation.toString() );
		}
		reader.close();
		directory.close();
	}

}
